<?php

/**
 * @package Skjb_Library
 */

/**
 * FFmpeg command line builder
 *
 * @package Skjb_Library
 */
class Skjb_Video_Ffmpeg
{
    /**
     * The full path to the FFmpeg executable
     * 
     * @var string
     */
    public $path = null;
    
    /**
     * The input file or path
     * 
     * @var string
     */
    public $input = null;
    
    /**
     * The output file or path
     * 
     * @var string
     */
    public $output = null;
    
    /**
     * Output at the same quality as the input
     * Adds -sameq to the command line
     * 
     * @var boolean
     */
    public $sameQuality = false;
    
    /**
     * Sets the verbosity level
     * Adds -v ? to the command line
     * 
     * @var integer
     */
    public $verbosity = null;
    
    /**
     * Disable the video output stream
     * Adds -vn to the command line
     * 
     * @var boolean
     */
    public $disableVideo = false;
    
    /**
     * Set the output format
     * Adds -f ? to the command line
     * 
     * @var string
     */
    public $format = null;
    
    /**
     * Sets the number of audio channels
     * Adds -ac ? to the command line
     * 
     * @var integer
     */
    public $audioChannels = null;
    
    /**
     * Sets the audio frequency in hz
     * Adds -ar ? to the command line
     * 
     * @var integer
     */
    public $audioFrequency = null;
    
    /**
     * Sets the audio bitrate
     * Adds -ab ? to the command line
     * 
     * @var integer
     */
    public $audioBitrate = null;
    
    /**
     * Sets the input framerate
     * Put -r ? before the input file in the command line
     * 
     * @var integer
     */
    public $inFrameRate = null;
    
    /**
     * Sets the framerate
     * Adds -r ? to the command line
     * 
     * @var integer
     */
    public $frameRate = null;
    
    /**
     * Disables the audio output stream
     * Adds -an to the command line
     * 
     * @var boolean
     */
    public $disableAudio = false;
    
    /**
     * Forces FFmpeg to overwrite output files
     * Adds -y to the command line
     * 
     * @var boolean
     */
    public $overwriteOutput = false;
    
    /**
     * Seek ahead in the input stream
     * Adds -ss ? to the command line
     * 
     * @var string
     */
    public $seekSeconds = null;
    
    /**
     * Limit output to this duration
     * Adds -t ? to the command line
     * 
     * @var string
     */
    public $duration = null;
    
    /**
     * The last command that was executed
     * 
     * @var string
     */
    public $lastCommand = null;
    
    /**
     * Instantiate and set the path of the FFmpeg executable
     * 
     * @param string $path
     */
    public function __construct($path)
    {
        $this->path = $path;
    }
    
    /**
     * Reset all of the settings to their defaults
     * 
     * @return null
     */
    public function reset()
    {
        $this->sameQuality = false;
        $this->verbosity = null;
        $this->disableVideo = false;
        $this->format = null;
        $this->audioChannels = null;
        $this->audioFrequency = null;
        $this->audioBitrate = null;
        $this->inFrameRate = null;
        $this->frameRate = null;
        $this->disableAudio = false;
        $this->overwriteOutput = false;
        $this->seekSeconds = null;
        $this->duration = null;
    }
    
    /**
     * Run the FFmpeg command as configured
     * 
     * @return string
     */
    public function run()
    {
        $command = $this->path;
        if ($this->inFrameRate !== null) {
            $command .= ' -r ' . $this->inFrameRate;
        }
        $command .= ' -i "' . $this->input . '"';
        if ($this->sameQuality) {
            $command .= ' -sameq';
        }
        if ($this->verbosity !== null) {
            $command .= ' -v ' . $this->verbosity;
        }
        if ($this->disableVideo) {
            $command .= ' -vn';
        }
        if ($this->format !== null) {
            $command .= ' -f ' . $this->format;
        }
        if ($this->audioChannels !== null) {
            $command .= ' -ac ' . $this->audioChannels;
        }
        if ($this->audioFrequency !== null) {
            $command .= ' -ar ' . $this->audioFrequency;
        }
        if ($this->audioBitrate !== null) {
            $command .= ' -ab ' . $this->audioBitrate;
        }
        if ($this->disableAudio) {
            $command .= ' -an';
        }
        if ($this->overwriteOutput) {
            $command .= ' -y';
        }
        if ($this->seekSeconds !== null) {
            $command .= ' -ss ' . $this->seekSeconds;
        }
        if ($this->duration !== null) {
            $command .= ' -t ' . $this->duration;
        }
        if ($this->frameRate !== null) {
            $command .= ' -r ' . $this->frameRate;
        }
        $command .= ' "' . $this->output . '"';
        $this->lastCommand = $command;
        $result = shell_exec($command);
        return $result;
    }
}